package user

import (
	"context"
	"git.mycaigou.com/gfyx/common/rpc_client"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/common/constants"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/common/errcode"
	microGfyxOpptyService "git.mycaigou.com/gfyx/micro-gfyx-api/proto/micro_gfyx_oppty_service_proto/micro-gfyx-oppty-service/base"
	microGfyxUserService "git.mycaigou.com/gfyx/micro-gfyx-api/proto/micro_gfyx_user_service_proto/micro-gfyx-user-service"
	"git.myscrm.cn/golang/stark/v4"
)

type LoginRepository struct{}

// NewLoginRepository 构造函数
func NewLoginRepository() LoginRepositoryIface {
	return &LoginRepository{}
}

func (l *LoginRepository) UnionUserLogin(ctx context.Context, tenantCode, account, password string) (*UnionUserLoginInfo, error) {
	request := &microGfyxUserService.LoginByAccountPasswordRequest{
		TenantCode: tenantCode,
		Account:    account,
		Password:   password,
	}

	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_USER_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := microGfyxUserService.NewUserLoginServiceClient(conn)
	res, err := client.LoginByAccountPassword(ctx, request)
	if err != nil {
		return nil, err
	}

	unionUserLoginInfo := UnionUserLoginInfo{}
	if res.UserInfo != nil {
		res.TenantInfo[1].GetTenantName()
		unionUserLoginInfo.UserInfo = &UnionUserInfo{
			UserName: res.UserInfo.UserName,
			Uid:      res.UserInfo.Uid,
			UserCode: res.UserInfo.UserCode,
			// todo
			//Logo:       res.UserInfo.Logo,
			//Mobile:     res.UserInfo.Mobile,
		}
		if res.TenantInfo != nil && len(res.TenantInfo) > 0 {
			unionUserLoginInfo.UserInfo.TenantCode = res.TenantInfo[0].GetTenantCode()
			unionUserLoginInfo.UserInfo.TenantName = res.TenantInfo[0].GetTenantName()
		}
	}

	unionUserLoginInfo.Token = res.Token

	return &unionUserLoginInfo, nil
}

func (l *LoginRepository) LoginByPassword(ctx context.Context, request *microGfyxOpptyService.LoginByPasswordRequest) (*microGfyxOpptyService.LoginByPasswordResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := microGfyxOpptyService.NewUserLoginServiceClient(conn)
	res, err := client.LoginByPassword(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "call grpc LoginByPhone request:%+v, err:%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

// LoginByPhone 验证码登录
func (l *LoginRepository) LoginByPhone(ctx context.Context, request *microGfyxOpptyService.LoginByPhoneRequest) (*microGfyxOpptyService.LoginByPhoneResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := microGfyxOpptyService.NewUserLoginServiceClient(conn)
	res, err := client.LoginByPhone(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "call grpc LoginByPhone request:%+v, err:%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

// ResetPasswordByPhone 重设密码
func (l *LoginRepository) ResetPasswordByPhone(ctx context.Context, request *microGfyxOpptyService.ResetPasswordByPhoneRequest) (*microGfyxOpptyService.ResetPasswordByPhoneResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := microGfyxOpptyService.NewUserLoginServiceClient(conn)
	res, err := client.ResetPasswordByPhone(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "call grpc ResetPasswordByPhone request:%+v, err:%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

func (l *LoginRepository) GetResetPasswordToken(ctx context.Context, request *microGfyxOpptyService.GetResetPasswordTokenRequest) (*microGfyxOpptyService.GetResetPasswordTokenResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := microGfyxOpptyService.NewUserLoginServiceClient(conn)
	res, err := client.GetResetPasswordToken(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "call grpc GetResetPasswordToken request:%+v, err:%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

// Sso 单点登录
func (l *LoginRepository) Sso(ctx context.Context, request *microGfyxOpptyService.SsoRequest) (*microGfyxOpptyService.SsoResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := microGfyxOpptyService.NewUserLoginServiceClient(conn)
	res, err := client.Sso(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "call grpc Sso request:%+v, err:%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

// LoginUserInfo 获取登录用户信息
func (l *LoginRepository) LoginUserInfo(ctx context.Context, request *microGfyxOpptyService.LoginUserRequest) (*microGfyxOpptyService.LoginUserInfo, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := microGfyxOpptyService.NewUserLoginServiceClient(conn)
	res, err := client.GetLoginUser(ctx, request)
	if err != nil {
		grpcErr := rpc_client.GetGrpcError(err)
		if grpcErr.GetCode() != errcode.COMMON_USER_TOKEN_EXPIRED {
			stark.Logger.Errorf(ctx, "call grpc LoginUserInfo request:%+v, err:%s", request, err.Error())
		}
		return nil, err
	}
	return res, nil
}

// LogoutUser 退出登录
func (l *LoginRepository) LogoutUser(ctx context.Context, request *microGfyxOpptyService.LogoutRequest) (*microGfyxOpptyService.LogoutResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := microGfyxOpptyService.NewUserLoginServiceClient(conn)
	res, err := client.Logout(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "call grpc LogoutUser request:%+v, err:%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

// RefreshToken 刷新用户 token
func (l *LoginRepository) RefreshToken(ctx context.Context, request *microGfyxOpptyService.RefreshTokenRequest) (*microGfyxOpptyService.RefreshTokenResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := microGfyxOpptyService.NewUserLoginServiceClient(conn)
	res, err := client.RefreshToken(ctx, request)
	if err != nil {
		// 区别下错误日志
		grpcErr := rpc_client.GetGrpcError(err)
		if grpcErr.GetCode() != errcode.USER_LOGIN_REFRESH_TOKEN_UNEXPIRE_ERROR {
			stark.Logger.Errorf(ctx, "call grpc RefreshToken request:%+v, err:%s", request, err.Error())
		}
		return nil, err
	}
	return res, nil
}
